﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using Southridge.Data;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;

namespace Southridge.View
{

    
    public class RibbonItem : RibbonCommand  , INotifyPropertyChanged 
    {
        public ICommand _actualCommand;

        protected event EventHandler _canExecuteChanged;
        ///<summary>
        ///Occurs when changes occur that affect whether or not the command should execute.
        ///</summary>
        ///
        protected bool _registerForCommandManager = false ;
        protected static int staticCount = 0; 
        protected int _currentCommand = ++staticCount; 

        public new  event EventHandler CanExecuteChanged
        {
            add
            {
                if (_registerForCommandManager)
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("Ribbonitem Listener for " + _currentCommand);
#endif
                    CommandManager.RequerySuggested += value;
                    _canExecuteChanged += value;
                }

            }
            remove
            {
                if (_registerForCommandManager)
                {
                    CommandManager.RequerySuggested -= value;
                    _canExecuteChanged += value;
                }
            }
        }




        public ICommand ActualCommand
        {

            get
            {
                return _actualCommand;
            }
            set
            {
                if (_actualCommand != value)
                {
                    ICommand oldCommand = _actualCommand;
                    _actualCommand = value;

                    //New command and old was null, subscribe 
                    if (_actualCommand != null && oldCommand == null)
                    {
                        this.CanExecute += new CanExecuteRoutedEventHandler(RibbonItem_CanExecute);
                        this.Executed += new ExecutedRoutedEventHandler(RibbonItem_Executed);
                    }
                    // not null new command and old was not null, do nothing 
                    // new command is null but oldcommand was not, so unsubscribe 
                    else if (_actualCommand == null && oldCommand != null)
                    {
                        this.CanExecute -= new CanExecuteRoutedEventHandler(RibbonItem_CanExecute);
                        this.Executed -= new ExecutedRoutedEventHandler(RibbonItem_Executed);
                    }

                    OnPropertyChanged("ActualCommand");
                }
            }
        }

        void RibbonItem_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (_actualCommand != null)
            {
                _actualCommand.Execute(e.Parameter); 
            } 
        }

        void RibbonItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (_actualCommand != null)
            {
                e.CanExecute = _actualCommand.CanExecute(e.Parameter);
                e.Handled = true; 
            } 
        }



       
        #region INotifyPropertyChanged Members

        public void OnPropertyChanged (string property)
        {
            if (m_propertyChanged != null)
                m_propertyChanged(this, new PropertyChangedEventArgs(property));
        }

        PropertyChangedEventHandler m_propertyChanged;

        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
        {
            add
            {
                base.PropertyChanged += value;
                m_propertyChanged += value;
            }
            remove
            {
                base.PropertyChanged -= value;
                m_propertyChanged -= value;
            }
        }

        #endregion 




    } 

    public class RibbonItem2 : BusinessEntity  
    {
        protected string _labelDescription; 
        public string LabelDescription 
        {
            get { return _labelDescription; }
            set {                
                if (value != _labelDescription)
                {
                    _labelDescription = value;
                    OnPropertyChanged("LabelDescription");
                } 
            } 
        }

        protected string _labelTitle;         
        public string LabelTitle
        {
            get { return _labelTitle; }
            set { _labelTitle = value; OnPropertyChanged("LabelTitle"); }
        }

        protected ImageSource _largeImageSource;
        public ImageSource LargeImageSource
        {
            get { return _largeImageSource; }
            set
            {
                if (value != _largeImageSource)
                {
                    _largeImageSource = value;
                    OnPropertyChanged("LargeImageSource");
                }
            }                 
        } 

       
        protected ImageSource _smallImageSource;
        public ImageSource SmallImageSource
        {
            get { return _smallImageSource; }
            set
            {
                if (value != _smallImageSource)
                {
                    _smallImageSource = value;
                    OnPropertyChanged("SmallImageSource");
                }
            }
        }

        protected string _toolTipDescription; 
        public string ToolTipDescription         
        {
            get { return _toolTipDescription; }
            set
            {
                if (value != _toolTipDescription)
                {
                    _toolTipDescription = value;
                    OnPropertyChanged("ToolTipDescription");
                }
            }
        }

        protected string _toolTipFooterDescription;
        public string ToolTipFooterDescription
        {
            get { return _toolTipFooterDescription; }
            set
            {
                if (value != _toolTipFooterDescription)
                {
                    _toolTipFooterDescription = value;
                    OnPropertyChanged("ToolTipFooterDescription");
                }
            }
        }

        protected ImageSource _tooltipFooterImageSource;
        public ImageSource ToolTipFooterImageSource
        {
            get { return _tooltipFooterImageSource; }
            set
            {
                if (value != _tooltipFooterImageSource)
                {
                    _tooltipFooterImageSource = value;
                    OnPropertyChanged("TooltipFooterImageSource");
                }
            }
        }


        protected string _toolTipFooterTitle;
        public string ToolTipFooterTitle
        {
            get { return _toolTipFooterTitle; }
            set
            {
                if (value != _toolTipFooterTitle)
                {
                    _toolTipFooterTitle = value;
                    OnPropertyChanged("ToolTipFooterTitle");
                }
            }
        }

        public ImageSource ToolTipImageSource { get; set; }


        protected string _toolTipTitle;
        public string ToolTipTitle
        {
            get { return _toolTipTitle; }
            set
            {
                if (value != _toolTipTitle)
                {
                    _toolTipTitle = value;
                    OnPropertyChanged("ToolTipTitle");
                }
            }
        }
 
        protected ICommand _command; 

        public ICommand Command
        {
            get
            {
                return _command; 
            }
            set
            {
                ICommand old = _command; 
                _command = value;
                if (_command != old)
                    OnPropertyChanged("Command"); 
            } 
        } 
    }
}
